package renda.dao.serivce.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import renda.common.Constants;
import renda.dao.TopicsDAO;
import renda.dao.UserDAO;
import renda.dao.serivce.TopicsService;
import renda.pojo.Topics;

@Service("topicsService")
public class TopicsServiceImpl implements TopicsService {

	@Resource(name = "topicsDAO")
	private TopicsDAO topicsDAO;

	@Resource(name = "userDao")
	private UserDAO userDAO;

	public TopicsDAO getTopicsDAO() {
		return topicsDAO;
	}

	public void setTopicsDAO(TopicsDAO topicsDAO) {
		this.topicsDAO = topicsDAO;
	}

	public List<Topics> listTopics(String name, Integer userId, int offset, int length) {
		List<Topics> topics = topicsDAO.listTopics(name, userId, offset, length);
		if (topics != null && topics.size() > 0) {
			for (Topics topics2 : topics) {
				topics2.setUname(userDAO.getNameById(topics2.getUid()));
			}
			return topics;
		}
		return Collections.emptyList();
	}

	public Topics saveTopics(Topics topics) {
		return topicsDAO.saveTopics(topics);
	}

	public Topics getById(Integer topicsId) {
		return topicsDAO.getById(topicsId);
	}

	public int countTopics(String name, Integer userId) {
		return topicsDAO.countTopics(name, userId);
	}

	@Override
	public int updateTopics(Integer topicsId, Integer userType) {
		if (Constants.USER_TYPE_ADMIN.equals(userType)) {
			topicsDAO.updateCount(topicsId, false);
			return 1;
		} else if (Constants.USER_TYPE_USER.equals(userType)) {
			topicsDAO.updateCount(topicsId, true);
			return 1;
		}
		return 0;
	}

}
